package zky410;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class str1 {
    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        List<String> list = new ArrayList<>();
        list.add(sc.readLine());
        String res;
        Map<String, String> map = new HashMap<>();
        List<String> aaa = new ArrayList<>();
        while ((res = sc.readLine()) != null && res.length() != 0) {
            String[] str = res.split(",");
            if (str.length != 1) {
                StringBuilder temp = new StringBuilder();
                for (int i = 1; i < str.length; i++) {
                    temp.append(str[i]);
                }
                map.put(temp.toString(), str[0]);
            } else {
                aaa.add(res);
            }
        }

        Set<String> set = new HashSet<>();
        String[] split = list.get(0).split(",");

        get(list, map, set);


        if (set.size() == 0) {
            System.out.println(-1);
        } else {
            int cc = 0;
            for (String s : set) {

                cc += Integer.parseInt(s);
            }
            System.out.println(cc);
        }
    }

    public static void get(List<String> list, Map<String, String> map, Set<String> res) {
        if (list.size() == 0) {
            return;
        }
        List<String> aa = new ArrayList<>();
        for (String temp : list) {
            for (String s : map.keySet()) {
                String bb = map.get(s);
                if (s.contains(temp) && !res.contains(bb)) {
                    res.add(bb);
                    aa.add(bb);
                }
            }
        }
        get(aa, map, res);
    }
}
